﻿define(function (require, exports, module) {
    var Config = require('config');
    var ImageStorage = require('imageStorage');
    var Canvas = require('canvas');
    var Initialization = require('initialization');
    var Request = require('request');
    var m_playerCanvas = Canvas.Get(3);
    var m_data;
    var m_x = 10;
    var m_y = 10;
    var m_width = 150;
    var m_height = 72;

    exports.Init = function (data) {
        m_data = data;
    };

    exports.ChooseCity = function (city) {
        m_data.Cities.hasOwnProperty(city.GetId()) && city.Enter(m_data.Cities[city.GetId()]);
    };

    exports.Show = function () {
        console.log(m_data);
        RenderAsync().start();
        m_playerCanvas.Bind(module.id, {
            click: function (e) {
                if (m_x <= e.clientX && e.clientX <= (m_x + m_width) && m_y <= e.clientY && e.clientY <= (m_y + m_height)) {
                    return false;
                }
            }
        });
    };

    var RenderAsync = eval(Wind.compile('async', function (_this) {
        m_playerCanvas.Call('save');
        m_playerCanvas.Set('fillStyle', 'rgba(195, 195, 195, 0.8)');
        m_playerCanvas.Call('fillRect', m_x, m_y, m_width, m_height);
        m_playerCanvas.Set('fillStyle', 'white')
            .Set('textAlign', 'left')
            .Set('textBaseline', 'top');
        m_playerCanvas.ChangeFont(12, true);
        m_playerCanvas.Call('fillText', m_data.Name, 85, 15);
        m_playerCanvas.Set('fillStyle', 'yellow');
        m_playerCanvas.Call('fillText', m_data.NickName, 85, 30);
        m_playerCanvas.Call('restore');

        var img = $await(ImageStorage.GetAsync(m_data.Img));
        m_playerCanvas.Call('drawImage', img, 15, 15);
    }));

    exports.Hide = function () {
        m_playerCanvas.Unbind();
        m_playerCanvas.Clear();
    };
});